package com.android.scanner.fragment;

import com.android.scanner.R;
import com.android.scanner.utils.Constant;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginFragment extends BaseFragment {
	private Context context;
	private EditText edtName, edtPass;
	private Button btnLogin;
	private Handler handler;
	private static LoginFragment mSelft = null;

	public static LoginFragment getInstance(Handler handler) {
		if (mSelft != null)
			return mSelft;
		return new LoginFragment(handler);
	}

	public LoginFragment(Handler handler) {
		this.handler = handler;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context = getActivity();
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_login, null);
		initViews(view);
		return view;
	}

	private void initViews(View view) {
		// TODO Auto-generated method stub
		edtName = (EditText) view.findViewById(R.id.edt_name);
		edtPass = (EditText) view.findViewById(R.id.edt_pass);
		btnLogin = (Button) view.findViewById(R.id.btn_login);

		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (edtName.getText().toString().length() <= 0
						|| edtPass.getText().toString().length() <= 0) {
					Toast.makeText(context, R.string.notnull,
							Toast.LENGTH_SHORT).show();
					return;
				}
				
				hideSystemKeyBoard(edtPass);
				// Map map = new LinkedHashMap<String, String>();
				// map.put("method", SoapUtil.METHOD_LOGIN);
				// map.put("action", SoapUtil.ACTION_LOGIN);
				// map.put("loginName", edtName.getText().toString());
				// map.put("password", edtPass.getText().toString());
				//
				// SoapUtil.runMethod(map, handler);

				handler.sendEmptyMessage(Constant.MSG_MAIN_PAGE);
			}
		});
	}

	public void hideSystemKeyBoard(View v) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}
}
